# Copyright (c) 2009 Daropia Project (www.daropia.org)
#
# This file is part of Daropia and is available under the terms
# of the GNU General Public Licence version 2.0 or later.


# Represent a topic
class Topic < Sequel::Model
    # A topic is owned by a forum
    many_to_one :forum

    # Each topic can have multiple posts
    one_to_many :posts
    
    def increment_message_count
        self.post_count += 1
        forum.post_count += 1
        forum.save
        save
    end
    
    def decrement_message_count
        self.post_count -= 1
        forum.post_count -= 1
        forum.save
        save
    end
    
    def before_create
        super
        forum.topic_count += 1
        forum.save
    end
    
    def after_destroy
        super
        forum.topic_count -= 1
        forum.post_count -= post_count
        forum.save
    end
    
end
